package com.shen.accountbook.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.shen.accountbook.R;
import com.shen.accountbook.bean.temp.FirstSortBean;
import com.shen.accountbook.bean.temp.SecondSortBean;
import com.shen.accountbook.bean.temp.ThirdSortBean;
import com.shen.accountbook.config.Constant;
import com.shen.accountbook.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 *
 *
 */
public class AddSortDialog<T> extends Dialog {

    @BindView(R.id.tv_title_dialogaddsort)
    TextView mTvTitle;

    @BindView(R.id.tv_first_sort_dialogaddsort)
    TextView mTvFirstSort;
    @BindView(R.id.tv_second_sort_dialogaddsort)
    TextView mTvSecondSort;
    @BindView(R.id.et_third_sort_dialogaddsort)
    EditText mEtThirdSort;


    Context mContext;
    T mBean;

    FirstSortBean mFirstSortBean;
    SecondSortBean mSecondSortBean;

    @BindView(R.id.tv_cancel_dialogaddsort)
    TextView mTvCancel;
    @BindView(R.id.tv_confirm_dialogaddsort)
    TextView mTvConfirm;



    public AddSortDialog(@NonNull Context context, T bean) {
        super(context, R.style.alert_dialog);
        setContentView(R.layout.dialog_add_sort);
        ButterKnife.bind(this);

        mContext = context;

        mBean = bean;
        init(bean);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 安卓弹出ProgressDialog进度框之后触摸屏幕就消失了的解决方法
        setCanceledOnTouchOutside(false);

//        // 设置此窗口的设置
//        Window window = this.getWindow();
//        WindowManager.LayoutParams params = window.getAttributes();
//
//        // params.flags = 0x01810100; 这个是能使用"edittext"和"button"
//        // WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN  ==>0x00000100 ==> 设置了这个;
//        // 弹出输入法时，不会将"对话框"顶上去
//        params.flags = WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |
//                WindowManager.LayoutParams.FLAG_SPLIT_TOUCH |
//                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
//        // params.alpha = (float) 0.5;  //透明度(背景)     （0.0-1.0）
//        window.setAttributes(params);
    }

    private void init(T bean) {
        if(bean == null){
            setFirstView();
        }else if(bean instanceof  FirstSortBean){
            setSecondView((FirstSortBean) bean);
        }else if(bean instanceof SecondSortBean){
            setThirdView((SecondSortBean) bean);
        }else if(bean instanceof ThirdSortBean){

        }
    }


    private void setFirstView() {
        mTvTitle.setText("添加一级分类");
        mTvFirstSort.setVisibility(View.GONE);
        mTvSecondSort.setVisibility(View.GONE);
    }

    private void setSecondView(FirstSortBean firstSortBean) {
        mTvTitle.setText("添加二级分类");
        mTvFirstSort.setVisibility(View.VISIBLE);
        mTvSecondSort.setVisibility(View.GONE);


        if (firstSortBean != null) {
            mTvFirstSort.setText(mContext.getString(R.string.first_sort,
                    firstSortBean.getFirst_sort_name()));
        }
    }

    private void setThirdView(SecondSortBean secondSortBean) {
        mTvTitle.setText("添加三级分类");
        mTvFirstSort.setVisibility(View.VISIBLE);
        mTvSecondSort.setVisibility(View.VISIBLE);

        if (secondSortBean != null) {
            mTvFirstSort.setText(mContext.getString(R.string.first_sort,
                    secondSortBean.getFirstSortBean().getFirst_sort_name()));
            mTvSecondSort.setText(mContext.getString(R.string.second_sort,
                    secondSortBean.getSecond_sort_name()));
        }
    }

    private CallBack callBack;

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    @OnClick({R.id.tv_cancel_dialogaddsort, R.id.tv_confirm_dialogaddsort})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel_dialogaddsort:
                onClickCancel();
                break;

            case R.id.tv_confirm_dialogaddsort:
                onClickConfirm();
                break;
        }
    }



    private void onClickCancel(){
        if (callBack != null)
            callBack.onClickLeftBtn();
        dismiss();
    }

    private void onClickConfirm(){
        if(mEtThirdSort != null){
            String name = mEtThirdSort.getText().toString().trim();
            if(StringUtils.isNotEmpty(name)){
                if (callBack != null)
                    callBack.onClickRightBtn(mBean, name);
                dismiss();
            }
        }

    }

    public interface CallBack {
        void onClickLeftBtn();

        <K> void onClickRightBtn(K bean, String name);
    }
}
